package pl.edu.pw.ee.buildcity.scm.subversion;

import org.fest.assertions.Fail;
import org.junit.Test;
import pl.edu.pw.ee.buildcity.commons.exceptions.BuildCityException;
import pl.edu.pw.ee.buildcity.commons.exceptions.BuildCityRuntimeException;

import java.net.URISyntaxException;

import static org.fest.assertions.Assertions.assertThat;
import static org.tmatesoft.svn.core.wc.SVNRevision.HEAD;
import static pl.edu.pw.ee.buildcity.scm.SCMConnector.HTTP;
import static pl.edu.pw.ee.buildcity.scm.SCMConnector.SVN;

/**
 * @author Mateusz
 * @since 2010-01-09
 */
public class SVNCheckoutParametersTestCase {

	@Test
	public void shouldParseHTTPConnector() {
		//Given
		final String url = "http://buildcity.googlecode.com/svn/trunk/wordcounter";

		//When
		SVNCheckoutParameters checkoutParameters = SVNCheckoutParameters.checkoutHEAD(url);

		//Then
		assertThat(checkoutParameters).isNotNull();
		assertThat(checkoutParameters.connector).isEqualTo(HTTP);
	}

	@Test
	public void shouldParseSVNConnector() {
		//Given
		final String url = "svn://code.google.com/p/buildcity/source/browse/svn/trunk/wordcounter";

		//When
		SVNCheckoutParameters checkoutParameters = SVNCheckoutParameters.checkoutHEAD(url);

		//Then
		assertThat(checkoutParameters).isNotNull();
		assertThat(checkoutParameters.connector).isEqualTo(SVN);
	}

	@Test
	public void shouldParseHost() {
		//Given
		final String url = "http://buildcity.googlecode.com/svn/trunk/wordcounter";

		//When
		SVNCheckoutParameters checkoutParameters = SVNCheckoutParameters.checkoutHEAD(url);

		//Then
		assertThat(checkoutParameters).isNotNull();
		assertThat(checkoutParameters.host).isEqualTo("buildcity.googlecode.com");
	}

	@Test
	public void shouldParsePort() {
		//Given
		final String url = "http://buildcity.googlecode.com/svn/trunk/wordcounter";

		//When
		SVNCheckoutParameters checkoutParameters = SVNCheckoutParameters.checkoutHEAD(url);

		//Then
		assertThat(checkoutParameters).isNotNull();
		assertThat(checkoutParameters.port).isEqualTo(80);
	}

	@Test
	public void shouldUseDefaultHttpPortIfNotSpecified() {
		//Given
		final String url = "http://buildcity.googlecode.com/svn/trunk/wordcounter";

		//When
		SVNCheckoutParameters checkoutParameters = SVNCheckoutParameters.checkoutHEAD(url);

		//Then
		assertThat(checkoutParameters).isNotNull();
		assertThat(checkoutParameters.port).isEqualTo(80);
	}

	@Test
	public void shouldUseDefaultSvnPortIfNotSpecified() {
		//Given
		final String url = "svn://code.google.com/p/buildcity/source/browse/svn/trunk/wordcounter";

		//When
		SVNCheckoutParameters checkoutParameters = SVNCheckoutParameters.checkoutHEAD(url);

		//Then
		assertThat(checkoutParameters).isNotNull();
		assertThat(checkoutParameters.port).isEqualTo(3690);
	}

	@Test
	public void shouldParsePath() {
		//Given
		final String url = "http://buildcity.googlecode.com/svn/trunk/wordcounter";

		//When
		SVNCheckoutParameters checkoutParameters = SVNCheckoutParameters.checkoutHEAD(url);

		//Then
		assertThat(checkoutParameters).isNotNull();
		assertThat(checkoutParameters.pathToCheckout).isEqualTo("/svn/trunk/wordcounter");
	}

	@Test
	public void shouldSetHEADRevision() {
		//Given
		final String url = "http://buildcity.googlecode.com/svn/trunk/wordcounter";

		//When
		SVNCheckoutParameters checkoutParameters = SVNCheckoutParameters.checkoutHEAD(url);

		//Then
		assertThat(checkoutParameters).isNotNull();
		assertThat(checkoutParameters.revisionToCheckout).isEqualTo(HEAD);
	}

	@Test
	public void shouldThrowRuntimeExceptionWhenGivenUrlIsMalformed(){
	    //Given
	    String malformedUrl = "http:/buildcity.googlecode.com/svn/trunk/wordcounter";

		//When
		try{
			SVNCheckoutParameters.checkoutHEAD(malformedUrl);
			Fail.fail("Should throw exception due to malformed URL !!!");
		}catch(BuildCityRuntimeException e){
			//Then
			assertThat(e).isInstanceOf(BuildCityRuntimeException.class);
			assertThat(e.getMessage()).isEqualTo("Malformed url to checkout: http:/buildcity.googlecode.com/svn/trunk/wordcounter");
			assertThat(e.getCause()).isInstanceOf(URISyntaxException.class);
		}
	}
}
